package com.itheima.web;


import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import com.itheima.service.MenuService;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/menu")
public class MenuController {

    @Reference
    MenuService menuService;

    @RequestMapping("/getMenuList")
    public Result getMenuList() {
        org.springframework.security.core.userdetails.User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if (user == null) {
            return Result.ERROR("请登录");
        }
        String username = user.getUsername();
        List<Menu> menuList = menuService.getMenuList(username);

        return Result.OK("", menuList);
    }

    @RequestMapping("/findAll.do")
    public Result findAll() {
        List<Menu> menuList = menuService.findAll();
        return Result.OK("", menuList);
    }

    @RequestMapping("/getMenu")
    public Result getMenu(@RequestParam(name = "id" ,required = false,defaultValue = "0")Integer id){
       List<Menu> menus= menuService.getMenus(id);

        return Result.OK("",menus);
    }

    @RequestMapping("/add")
    public Result  add(@RequestBody Menu menu){
        menuService.add(menu);
        return Result.OK("");
    }

}
